#!/bin/bash

image_name="registry.cn-shanghai.aliyuncs.com/zhph-server"
device="gpu"

# List of services to build
services=(
    # "comfyui"
    "embedding-server"
    # "funasr-server"
    "llm-transformer-server"
    # "maskgct"
    "rerank-server"
    # "webui"
    "wan21"
    "diffusers-image-server"
    "diffusers-video-server"
    "ollama"
)

# Create output directory
output_dir="docker-images-tar-cpu"
mkdir -p "$output_dir"

# File to store image list
image_list_file="$output_dir/image-list.txt"
> "$image_list_file"

# Loop through each service, pull and save the Docker image
for service in "${services[@]}"; do
    # Use cpu for ollama, gpu for other services
    if [ "$service" == "ollama" ]; then
        service_device="cpu"
    else
        service_device="$device"
    fi
    
    full_image="$image_name/$service:$service_device"
    echo "Pulling $full_image ..."
    docker pull "$full_image"
    tar_file="$output_dir/${service}_${service_device}.tar"
    echo "Saving $full_image to $tar_file ..."
    docker save -o "$tar_file" "$full_image"
    echo "$tar_file" >> "$image_list_file"
done

# Add extra image: auto_openai:shdx
extra_image="registry.cn-shanghai.aliyuncs.com/zhph-server/auto_openai:shdx"
extra_tar_file="$output_dir/auto_openai_shdx.tar"
echo "Pulling $extra_image ..."
docker pull "$extra_image"
echo "Saving $extra_image to $extra_tar_file ..."
docker save -o "$extra_tar_file" "$extra_image"
echo "$extra_tar_file" >> "$image_list_file"

echo "All images have been saved to $output_dir."
echo "Image list written to $image_list_file."
